package com.firetornado.android.gowallajava;

import java.text.SimpleDateFormat;
import java.util.List;

import com.firetornado.android.gowallajava.ImageOps;
import com.firetornado.android.gowallajava.GowallaJavaAuth;
import com.firetornado.android.gowallajava.R;
import com.ginsberg.gowalla.dto.Item;
import com.ginsberg.gowalla.dto.ItemEvent;
import com.ginsberg.gowalla.exception.GowallaException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This is an example of how to use gowalla-java within Android to call and display item details.
 * Item details and/or activity seems to require Auth, so try different items if you have issues with this one.
 * I picked this item because it has a ton of activity.
 * 
 * @author Ryan Crutchfield - rc6750@gmail.com
 * http://gowalla.com/users/rc6750
 *
 */
public class ItemsMain extends Activity {
	final Handler mHandler = new Handler(); 
	private Item itemData;
	private List<ItemEvent> itemevents;
	String theText;
	String theSpot;
	String drop = "drop";
	String vault = "vault";
	String bonus = "bonus";
	String pickup = "pick_up";
	static SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy");
	
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.itemsmain);
        final ProgressDialog pd = ProgressDialog.show(this,"Item Detail","Loading....",true, false);
   		new Thread(new Runnable(){
       		public void run(){
       	        try {
       	        	itemData = GowallaJavaAuth.gowallaBasicAuth.getItem(75865);
       			} catch (GowallaException e) {
       				// TODO Auto-generated catch block
       				e.printStackTrace();
       			}
       			mHandler.post(mUpdateResults);
        		pd.dismiss();
       		}
   		}).start();
    }
	
	final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi(itemData);
        }
    }; 
    
    final void updateResultsInUi(Item itemData)
    {
    	Drawable image = ImageOps.ImageOperations(itemData.getImageUrl(),"image.jpg");		
    	ImageView imgView = (ImageView)findViewById(R.id.itemImage);
		imgView.setImageDrawable(image);
    			
		TextView name = (TextView)findViewById(R.id.itemName);
		name.setText(itemData.getName());
		
		TextView issueNo = (TextView)findViewById(R.id.itemIssueNumber);
		issueNo.setText("#" + itemData.getIssueNumber());
		
		TextView itemActivity = (TextView)findViewById(R.id.itemActivity);
		itemActivity.setText("Item Activity:\n\n");
		
		try {
			itemevents =  GowallaJavaAuth.gowallaBasicAuth.getItemEvents(itemData.getId());
		} catch (GowallaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (ItemEvent event : itemevents)
		{
			if(event.getType().equals(drop))
			{
				theText = "Dropped by ";
				theSpot = " at " + event.getSpot().getName();
			}
			else if (event.getType().equals(vault))
			{
				theText = "Vaulted by ";
				theSpot = "";
			}
			else if (event.getType().equals(pickup))
			{
				theText = "Picked up by ";
				theSpot = " at " + event.getSpot().getName();
			}
			else if (event.getType().equals(bonus))
			{
				theText = "Bonus Item awarded to ";
				theSpot = "";
			}
			
			itemActivity.append(theText + event.getUser().getName() + theSpot + " on " + FORMATTER.format(event.getCreatedAt()) + "\n\n");
		}
    }
	
	
    		
}